package com.css.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.postgis.Geometry;
import org.postgis.PGgeometry;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Dbj
 * @date 2024年09月04日  16:29
 */
@MappedTypes({String.class})
public class GeometryTypeHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        PGgeometry pGgeometry = new PGgeometry(parameter);
        Geometry geometry = pGgeometry.getGeometry();
        geometry.setSrid(4490);
        pGgeometry.setGeometry(geometry);
        ps.setObject(i, pGgeometry);
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        PGgeometry pGgeometry = new PGgeometry(rs.getString(columnName));
        Geometry geometry = pGgeometry.getGeometry();
        geometry.setSrid(4490);
        pGgeometry.setGeometry(geometry);
        return pGgeometry.toString();
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        PGgeometry pGgeometry = new PGgeometry(rs.getString(columnIndex));
        Geometry geometry = pGgeometry.getGeometry();
        geometry.setSrid(4490);
        pGgeometry.setGeometry(geometry);
        return pGgeometry.toString();
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        PGgeometry pGgeometry = new PGgeometry(cs.getString(columnIndex));
        Geometry geometry = pGgeometry.getGeometry();
        geometry.setSrid(4490);
        pGgeometry.setGeometry(geometry);
        return pGgeometry.toString();
    }

}
